package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetMealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Slf4j
@Service
public class SetMealServiceImpl implements SetMealService {

    @Autowired
    private SetMealMapper setMealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //指定开始页长度
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        //调用持久层mapper
        Page<SetmealVO> page = setMealMapper.pageQuery(setmealPageQueryDTO);
        //创建记录数
        long total = page.getTotal();
        //创建返回数据
        List<SetmealVO> records = page.getResult();
        PageResult pageResult = new PageResult(total,records);
        return pageResult;
    }

    /**
     * 新增套餐
     * @param setmealVo
     */
    //开启事务锁
    @Transactional
    public void save(SetmealVO setmealVo) {
        //插入到套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVo, setmeal);
        //返回id
        setMealMapper.insert(setmeal);
        Long id = setmeal.getId();
        //插入到套餐菜品关系表
        //TODO 封装id
        List<SetmealDish> setmealDish =setmealVo.getSetmealDishes();
        setmealDish.forEach(dish -> {
            dish.setSetmealId(id);
        });
        setMealDishMapper.insert(setmealDish);
    }
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //查询套餐表
         setmealVO = setMealMapper.getById(id);
        //查询套餐关系表
        List<SetmealDish> setmealDish = setMealDishMapper.getById(id);
        //封装数据返回
        setmealVO.setSetmealDishes(setmealDish);
        return setmealVO;
    }

    /**
     * 修改套餐
     * @param setmealVo
     */
    //开启事务锁
    @Override
    public void update(SetmealVO setmealVo) {
        //修改套餐表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVo, setmeal);
        setMealMapper.update(setmeal);
        //修改套餐关系表
        List<SetmealDish> setmealDish = setmealVo.getSetmealDishes();
        //删除原有数据
        setMealDishMapper.deleteBySetmealId(setmeal.getId());
        //判断是否为空
        if(setmealDish != null && setmealDish.size() > 0){
            setmealDish.forEach(dish -> {
                dish.setSetmealId(setmealVo.getId());
            });
            setMealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否起售
        for (Long id : ids) {
            SetmealVO setmeal = setMealMapper.getById(id);
            if(setmeal.getStatus() == 1){
                //起售中的菜品不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //删除套餐表
        setMealMapper.deleteBatch(ids);
        //删除套餐关系表
        setMealDishMapper.deleteBatch(ids);
    }

    /**
     * 售卖状态修改
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setMealMapper.update(setmeal);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setMealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setMealMapper.getDishItemBySetmealId(id);
    }

}
